import moment from 'moment';
import * as XLSX from 'xlsx';
export function exportExcel(json: any, titleArr: any,contentArr:any, execlName:String) {
  var data = new Array();
  var keyArray = new Array();
  const getLength = function (obj) {
    var count = 0;
    for (var i in obj) {
      if (obj.hasOwnProperty(i)) {
        count++;
      }
    }
    return count;
  };
  // 获取数组的length
  for (const key1 in json) {
    if (json.hasOwnProperty(key1)) {
      const element = json[key1];
      var rowDataArray = new Array();
      for(let i = 0; i <contentArr.length;i++){
        rowDataArray[i]=element[contentArr[i]]
      }
      for (const key2 in element) {
        if (keyArray.length < getLength(element)) {
          keyArray.push(key2);
        }
      }
      data.push(rowDataArray);
    }
  }
  console.log('表格数据',keyArray,titleArr);
  
  data.splice(0, 0, keyArray, titleArr);
  const ws = XLSX.utils.aoa_to_sheet(data);
  const wb = XLSX.utils.book_new();
  var wsrows = [{hidden: true}];
  ws['!rows'] = wsrows;
  XLSX.utils.book_append_sheet(wb, ws, 'sheetName');
  XLSX.writeFile(wb, moment(new Date()).format('yyyy-MM-DD HH/mm/ss') + execlName + '.xlsx');
}
